package com.aitaoxue.palx.ui.register;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.InjectView;

import com.aitaoxue.palx.AppContext;
import com.aitaoxue.palx.AppManager;
import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.remote.AitaoXueApi;
import com.aitaoxue.palx.base.BaseActivity;
import com.aitaoxue.palx.ui.LoginActivity;
import com.aitaoxue.palx.util.MD5Utils;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class ForgetPasswordActivity2 extends BaseActivity {

	@InjectView(R.id.password)
	EditText password;
	@InjectView(R.id.re_password)
	EditText rePassword;
	@InjectView(R.id.alert_line)
	RelativeLayout alert;
	@InjectView(R.id.alert_content)
	TextView alertContent;
	@InjectView(R.id.btn_push)
	Button push;

	String md5pas;

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.forget_password;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_forget_password_2;
	}

	@Override
	public void onClick(View v) {
		if (TextUtils.isEmpty(password.getText().toString())) {
			showToast("请填入密码", 0, 0);
			return;
		}

		if (TextUtils.isEmpty(rePassword.getText().toString())) {
			showToast("请填入确认密码", 0, 0);
			return;
		}

		if (!TextUtils.equals(password.getText(), rePassword.getText())) {
			showError("两次密码不一致");
			return;
		}

		md5pas = MD5Utils.md5(password.getText().toString());
		AitaoXueApi.prodFindPsw3(md5pas, md5pas, mHandler);
	}

	@Override
	public void initView() {
		push.setOnClickListener(this);

		if (getIntent().getStringExtra("from").equals("forget")) {
			AppManager.getAppManager().addActivity(this);
		}
	}

	@Override
	public void initData() {

	}

	private void showError(String error) {
		if (alert.getVisibility() == View.GONE) {
			alert.setVisibility(View.VISIBLE);
		}
		alertContent.setText(error);
	}

	private AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			String res = new String(arg2);

			try {
				JSONObject json = new JSONObject(res);
				String code = json.getString("code");

				if (code.equals("0000")) {
					// showToast("修改成功", 0, 0);
					Toast.makeText(ForgetPasswordActivity2.this, "修改成功",
							Toast.LENGTH_SHORT).show();

					AppContext.getInstance().setProperty("user.password",
							md5pas);

					if (getIntent().getStringExtra("from").equals("forget")) {
						startActivity(new Intent(ForgetPasswordActivity2.this,
								LoginActivity.class));
					} else {
						finish();
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {
		}
	};

}
